<?php

class QuestionController extends Zend_Controller_Action
{

    public function init()
    {
        /* Initialize action controller here */
    }

    public function indexAction()
    {
                $db = Zend_Registry:: get('db');
                $select = $db->select()
                        ->from(array('q'=>'questions'))
                        ->join(array('u'=> 'users'),'u.id = q.author_id', 'username')
                        ->joinLeft(array('a'=>'answers'), 'a.question_id = q.id',array('answers_count'=>'COUNT(a.id)'))
                        ->group('q.id');
                //echo $select->__toString();
                $paginator = new Zend_Paginator(new Zend_Paginator_Adapter_DbSelect($select));
                $paginator->setCurrentPageNumber($this->_getParam('page'));
                $paginator->setItemCountPerPage(5);
                $this->view->paginator = $paginator;
                
        /*    $question = new Application_Model_Question();
        $this->view->question = $question->getAllQuestions();*/
    }

    public function addAction()
    {
        $form = new Application_Model_Form_Question();
        if($this->getRequest()->isPost()){
            if($form->isValid($this->getRequest()->getPost())){
                $question = new Application_Model_Question();
                $question->fill($form->getValues());
                $question->created = date('Y-m-d H:i:s');
                 $question->author_id = Zend_Auth::getInstance()->getIdentity()->id;
                $question->save();
                $this->_redirect('/question');
            }
        }
        $this->view->form = $form;
        
    }

    public function viewAction()
    {
        $id = $this->_getParam('id');
        $pageId = $this->_getParam('page');
        $question = new Application_Model_Question($id);
        $db = Zend_Registry:: get('db');
                $select = $db->select()
                        ->from(array('a'=>'answers'),array('answer','created'))
                        ->joinLeft(array('u'=> 'users'),'a.author_id = u.id', 'username')
                        ->joinLeft(array('q'=>'questions'), 'a.question_id = q.id', array('id', 'title'))
                        ->where('a.question_id ='.$id);
         $paginator = new Zend_Paginator(new Zend_Paginator_Adapter_DbSelect($select));
                $paginator->setCurrentPageNumber($this->_getParam('page'));
                $paginator->setItemCountPerPage(5);
                $this->view->paginator = $paginator;
         $form = new Application_Model_Form_Answer;
        		if ($this->getRequest()->isPost()) {
			if ($form->isValid($this->getRequest()->getPost())) {
				$answer = new Application_Model_Answer();
				$answer->fill($form->getValues());
				$answer->created = date('Y-m-d H:i:s');
				$answer->author_id = Zend_Auth::getInstance()->getIdentity()->id;
				$answer->question_id = $id;
				$answer->save();
                                $this->_redirect('/question/view/id/'.$id.'/page/'.$pageId);
			}
		}
		
		$this->view->form = $form;
		$this->view->question = $question;
		$this->view->author = $question->getAuthor();;
	}


}





